<template>
  <div>
    <a-modal
      title="发牌原因"
      :visible="visible"
      :confirm-loading="loading"
      ok-text="保存"
      @ok="ok"
      @cancel="cancel"
    >
      <div class="about-master-container">
        <a-form-model ref="form" :model="queryParam" :rules="rules">
          <a-row>
            <a-col span="24">
              <a-form-model-item label="分类">
                <BaseInput v-model="queryParam.typeName" disabled />
              </a-form-model-item>
            </a-col>
            <a-col span="24">
              <a-form-model-item label="发牌原因" prop="dealReason">
                <BaseTextarea v-model="queryParam.dealReason" placeholder="请输入" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col span="24">
              <a-form-model-item label="状态" prop="status">
                <a-select v-model="queryParam.status">
                  <a-select-option :value="0">可用</a-select-option>
                  <a-select-option :value="1">不可用</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-modal>
  </div>
</template>

<script>
import api from '@/api/safetySupervision/base/licensingReason'

export default {
  name: 'a' + Date.now(),
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    form: {
      type: Object,
      default: () => null
    }
  },
  data() {
    return {
      loading: false,
      queryParam: { status: 1, parentName: '' },
      rules: {
        dealReason: [{ required: true, message: '请输入发牌原因', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  watch: {
    visible(e) {
      if (e) {
        this.$nextTick(() => {
          this.queryParam = this.$clone(this.form)
        })
      } else {
        this.queryParam = { status: 1, parentName: '' }
        this.$refs.form.clearValidate()
      }
    }
  },
  methods: {
    async ok() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.loading = true
          let res
          let temp = this.$clone(this.queryParam)
          if (this.queryParam.id) {
            api.updateByPrimaryKeySelective(temp).then((req)=>{
                if(req.code === '0'){
                  this.$noticeSuccess('成功')
                  this.$emit('ok', temp)
                    this.cancel();
                 }

            }).catch((err)=>{

            })

          } else {
               api.addInsertSelective(temp).then((req)=>{
                 if(req.code === '0'){
                  this.$noticeSuccess('成功')
                  this.$emit('ok', temp)
                  this.cancel();
                 }

            }).catch((err)=>{
                  
            })
          }
          this.loading = false;
        } else {
          this.$noticeWarning('请填写完整')
        }
      })
    },
    cancel() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
